﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Comsenz.Base;
using Lbb.Web;
using Comsenz.Discuz.Configs;

namespace Comsenz.Discuz
{
    /// <summary>
    /// Discuz模块管理器
    /// </summary>
    /// <author>Chiron</author>
    public static class DiscuzBlocksManager
    {
        /// <summary>
        /// 通讯器
        /// </summary>
        private readonly static CommunicatorBase communicator = new DiscuzApiCommunicator();
        /// <summary>
        /// 执行操作
        /// </summary>
        private static string Do(string action, IEnumerable<QueryParameter> args = null, IEnumerable<QueryParameter> extArgs = null, bool throwError = false)
        {
            try
            {
                return communicator.Do("blocks", action, args, extArgs);
            }
            catch (Exception)
            {
                if (throwError) throw;
            }
            return string.Empty;
        }

        /// <summary>
        /// 取得模块内容
        /// </summary>
        /// <param name="blockId">模块标识</param>
        /// <returns>模块内容</returns>
        public static string GetBlockContent(int blockId)
        {
            return Do("getblockcontent", new[] { 
                new QueryParameter("bid",blockId)
            });
        }
        /// <summary>
        /// 取得模块内容
        /// </summary>
        /// <param name="blockId">模块配置名</param>
        /// <returns>模块内容</returns>
        public static string GetBlockContent(string blockName)
        {
            try
            {
                var bid = DiscuzConfig.Instance.Blocks[blockName].Value;

                return Do("getblockcontent", new[] { 
                    new QueryParameter("bid",bid)
                });
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
